//
//  vdTextureContainer.m
//  Techdemo
//
//  Created by Sidney Just on 07.02.10.
//  Copyright 2010 Apple Inc. All rights reserved.
//

#import "vdTextureContainer.h"


@implementation vdTextureContainer

- (void)addTextures:(NSArray *)texArray {
	int oldSize = count;
	
	count += [texArray count];
	texture = (vdTexNode **)realloc(texture, count*sizeof(vdTexNode *));
	
	for(int i=0; i<[texArray count]; i++)
	{
		if([[texArray objectAtIndex:i] isKindOfClass:[NSString class]])
		{
			texture[oldSize+i] = [[vdTexManager sharedManager] loadTexture:[texArray objectAtIndex:i]];
			
			if(texture[i] == NULL)
			{
				vdLog(@"Failed to load texture <%@>", [texArray objectAtIndex:i]);
			}
		} else {
			vdLog(@"Texture Array %@ contains no string at %i, can't load texture!", texArray, i);
		}
		
	}
}

- (void)addTexture:(NSString *)tex {
	count += 1;
	texture = (vdTexNode **)realloc(texture, count*sizeof(vdTexNode *));
	
	texture[count-1] = [[vdTexManager sharedManager] loadTexture:tex];
}

- (id)initWithTextures:(NSArray *)texArray {
	if(self = [super init])
	{
		vdLog(@"Created %@ with %i textures", self, [texArray count]);
		
		texture = (vdTexNode **)malloc([texArray count]*sizeof(vdTexNode *));
		count = [texArray count];
		
		for(int i=0; i<[texArray count]; i++)
		{
			if([[texArray objectAtIndex:i] isKindOfClass:[NSString class]])
			{
				texture[i] = [[vdTexManager sharedManager] loadTexture:[texArray objectAtIndex:i]];
				
				if(texture[i] == NULL)
				{
					vdLog(@"Failed to load texture <%@>", [texArray objectAtIndex:i]);
				}
			} else {
				vdLog(@"Texture Array %@ contains no string at %i, can't load texture!", texArray, i);
			}

		}
	}
	return self;
}

- (id)init {
	if(self = [super init])
	{
		vdLog(@"Created %@", self);
		
		texture = (vdTexNode **)malloc(sizeof(vdTexNode *));
		count = 0;
	}
	return self;
}

- (void)dealloc {
	vdLog(@"Deleted %@ containing %i textures", self, count);
	
	for(int i=0; i<count; i++)
	{
		[[vdTexManager sharedManager] deleteTextureNode:texture[i]];
	}
	
	free(texture);
	
	[super dealloc];
}

@end
